package com.itheima.visitor;

import org.objectweb.asm.*;

import java.util.Objects;


public class CommonClassVisitor extends ClassVisitor {
    boolean hasTarget = false;
    CommonMethodVisitor methodVisitor;
    private String methodName;

    public boolean achieveTarget() {
        if (methodVisitor != null) {
            setHasTarget(methodVisitor.hasTarget());
        }
        return hasTarget;
    }

    protected void setHasTarget(boolean hasTarget) {
        this.hasTarget = hasTarget;
    }

    public CommonClassVisitor(String methodName, CommonMethodVisitor visitor) {
        super(Opcodes.ASM9);
        this.methodName = methodName;
        this.methodVisitor = visitor;
    }

    public CommonClassVisitor(String methodName) {
        super(Opcodes.ASM9);
        this.methodName = methodName;
    }

    public CommonClassVisitor(CommonMethodVisitor methodVisitor) {
        super(Opcodes.ASM9);
        this.methodVisitor = methodVisitor;
    }

    @Override
    public MethodVisitor visitMethod(int access, String name, String descriptor, String signature, String[] exceptions) {
        if (!Objects.equals(methodName, name)) {
            return null;
        }

        return methodVisitor;
    }

}

